package com.example.mathtest;



import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;


public class SelectActivity extends BaseActivity {
    Toolbar toolbar;
    Button mButton1;
    Button mButton2;
    Button mButton3;
    Button mButton4;
    Button mButton5;
    Button mButton6;

    private int NumberOfQuestions = 0 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        toolbar = findViewById(R.id.toolbar_select);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_keyboard_arrow_left_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                finish();
            }
        });

        //初始化实例
        mButton1 = findViewById(R.id.level_one);
        mButton2 = findViewById(R.id.level_two);
        mButton3 = findViewById(R.id.level_three);
        mButton4 = findViewById(R.id.level_four);
        mButton5 = findViewById(R.id.level_five);
        mButton6 = findViewById(R.id.level_six);

        //连接所有的按钮
        mButton1.setOnClickListener(new ButtonListener());
        mButton2.setOnClickListener(new ButtonListener());
        mButton3.setOnClickListener(new ButtonListener());
        mButton4.setOnClickListener(new ButtonListener());
        mButton5.setOnClickListener(new ButtonListener());
        mButton6.setOnClickListener(new ButtonListener());

    }


    //统一处理六个按钮的点击事件
    private class ButtonListener implements  View.OnClickListener{
        public void onClick(View view){
            switch (view.getId()){
                case R.id.level_one:
//                    InputNumberOfQuestions();
                    Intent intent1 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent1.putExtra("level",1);
//                    intent1.putExtra("number",NumberOfQuestions);
                    startActivity(intent1);
                    break;
                case R.id.level_two:
                    Intent intent2 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent2.putExtra("level",2);
                    startActivity(intent2);
                    break;
                case R.id.level_three:
                    Intent intent3 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent3.putExtra("level",3);
                    startActivity(intent3);
                    break;
                case R.id.level_four:
                    Intent intent4 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent4.putExtra("level",4);
                    startActivity(intent4);
                    break;
                case R.id.level_five:
                    Intent intent5 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent5.putExtra("level",5);
                    startActivity(intent5);
                    break;
                case R.id.level_six:
                    Intent intent6 = new Intent(SelectActivity.this , QuestionsActivity.class);
                    intent6.putExtra("level",6);
                    startActivity(intent6);
                    break;
                default:
                    break;
            }
        }
    }


    //答题前选择答题数目,并检查输入的文本是否正确。
    public void InputNumberOfQuestions(){
        final EditText editText = new EditText(SelectActivity.this);
        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
        AlertDialog.Builder builder = new AlertDialog.Builder(SelectActivity.this);
        builder.setTitle("请输入你要做的题目的数量");
        builder.setView(editText);
        final Dialog dialog = builder.show();
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (editText.getText().toString().equals(" ")){
                    editText.setText("");
                    Toast.makeText(SelectActivity.this,"输入不可以为空",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
                else if (editText.getText().toString().equals("0")){
                    editText.setText("");
                    Toast.makeText(SelectActivity.this,"输入不可以为0",Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
                else {
                    String number = editText.getText().toString();
                    NumberOfQuestions = Integer.valueOf(number);
                }
            }
        });
        builder.setNegativeButton("取消", null);
    }

}
